//
//  RouteView.m
//  CarLifes
//
//  Created by yu on 2017/7/13.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "RouteView.h"

#import "RouteCell.h"

#define kRouteCellIdentifier @"RouteCellIdentifier"
#define kRouteIndicatorViewHeight   70.f

@implementation DriveRouteViewInfo

@end

@interface RouteView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    UIButton *_navigationBtn;
    UIView *_routesContainer;
    UICollectionView *_routeIndicatorView;
}

@end

@implementation RouteView

- (instancetype)init
{
    if (self = [super init])
    {
        self.layer.shadowRadius = 2;
        self.layer.shadowOpacity = 0.3;
        self.layer.shadowColor = UIColor.lightGrayColor.CGColor;
        
        [self setupNavigationView];
        
        [self initRouteIndicatorView];
    }
    return self;
}

- (void)setRoutes:(NSMutableArray *)routes
{
    _routes = routes;
    [_routeIndicatorView reloadData];
    
    _navigationBtn.hidden = NO;
}

- (void)setupNavigationView
{
    _navigationBtn = [[UIButton alloc] init];
    _navigationBtn.hidden = YES;
    [self addSubview:_navigationBtn];
    _navigationBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    _navigationBtn.backgroundColor = kBaseColor;
    [_navigationBtn setTitle:@"开始导航" forState:UIControlStateNormal];
    [_navigationBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_navigationBtn addTarget:self action:@selector(GPSNaviAction) forControlEvents:UIControlEventTouchUpInside];
    
    if (@available(iOS  13.0, *)) {
        UIColor *naviBtnColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                return kBaseColor;
            } else {
                return kBaseColorDark;
            }
        }];
        
        self.backgroundColor = [UIColor secondarySystemBackgroundColor];
        _navigationBtn.backgroundColor = naviBtnColor;
    } else {
        self.backgroundColor = [UIColor whiteColor];
        _navigationBtn.backgroundColor = kBaseColor;
    }

    _navigationBtn.sd_layout
    .rightSpaceToView(self, 8)
    .bottomSpaceToView(self, kIsIphoneX ? 34 : 15)
    .widthRatioToView(self, 0.28)
    .autoHeightRatio(0.4);
    
    _navigationBtn.sd_cornerRadius = @(1);
}

- (void)initRouteIndicatorView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.minimumLineSpacing = 8;
    layout.minimumInteritemSpacing = 0;
    
    _routeIndicatorView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - kRouteIndicatorViewHeight, CGRectGetWidth(self.bounds), kRouteIndicatorViewHeight) collectionViewLayout:layout];
    
    _routeIndicatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    _routeIndicatorView.backgroundColor = [UIColor clearColor];
    _routeIndicatorView.pagingEnabled = YES;

    _routeIndicatorView.showsVerticalScrollIndicator = NO;
    _routeIndicatorView.showsHorizontalScrollIndicator = NO;
    
    _routeIndicatorView.delegate = self;
    _routeIndicatorView.dataSource = self;
    
    [_routeIndicatorView registerNib:NIB_NAMED(@"RouteCell") forCellWithReuseIdentifier:kRouteCellIdentifier];
    
    [self addSubview:_routeIndicatorView];
    
    _routeIndicatorView.sd_layout
    .leftEqualToView(self)
    .topSpaceToView(self, 0)
    .rightEqualToView(self)
    .bottomSpaceToView(_navigationBtn, 0);
    
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.routes.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RouteCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kRouteCellIdentifier forIndexPath:indexPath];
    DriveRouteViewInfo *info = self.routes[indexPath.row];
    cell.strategyLabel.text = info.contentTitle;
    cell.timeLabel.text = info.timeTitle;
    cell.distanceLabel.text = info.mileageTitle;
    
    // 这个是在下一个runloop执行
    dispatch_async(dispatch_get_main_queue(), ^{
        [collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    });

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return self.routes.count == 1 ? CGSizeMake(kScreenWidth/2.5, kAutoHeight(80)) : CGSizeMake((kScreenWidth - (self.routes.count+1)*8)/self.routes.count, kAutoHeight(80));
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(8, 8, 8, 8);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DriveRouteViewInfo *info = self.routes[indexPath.row];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectNaviRouteMethodWithRouteId:)])
    {
        [self.delegate selectNaviRouteMethodWithRouteId:info.routeID];
    }
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    CLRouteCell *cell = (CLRouteCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
//    [cell resetCellBorderColor:[UIColor CLTextLightGrayColor] strategyLabelBgColor:[UIColor CLButtonGrayColor] strategyLabelTextColor:[UIColor baseCLTextBlackColor] timeLabelBgColor:[UIColor CLBackGroundLightGrayColor] timeLabelTextColor:[UIColor baseCLTextBlackColor] distanceLabelBgColor:[UIColor CLBackGroundLightGrayColor] distanceLabelTextColor:[UIColor baseCLTextBlackColor]];
//    PPLog(@"deSelect index = %ld",indexPath.row);
}

#pragma mark - GPSNaviAction

- (void)GPSNaviAction
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(startGPSNaviAction)]) {
        [self.delegate startGPSNaviAction];
    }
}


@end
